clear all
#delimit ;
version 11.2;
set mem 650m;
set matsize 800;
set maxvar 32767;
set more off;
cap log close;


foreach var in  housing /*restbis busloan*//*credcard*/  {;
foreach x in 1 2 3 4 5 { /* incorporate the rest of imputations */;


cd "H:\Mi unidad\Grant_new\EFF";

use sample_eff.dta, clear;
*svyset [pw=facine3_imp`x'], bsrweight(wt3r_*) vce(bootstrap);
svyset [pw=facine3_imp`x'];

drop if oth_inact_imp`x'==1 | disable_imp`x'==1 | student_imp`x'==1 | housew_imp`x'==1;
keep if age_imp`x'>=3 & age_imp`x' <=7.5;
drop if madult_imp`x'==1;

* Define household wealth variable in function of the type of debt considered;

rename `var'w1_imp`x' w1_imp`x';
rename `var'w2_imp`x' w2_imp`x';
rename `var'w3_imp`x' w3_imp`x';
rename `var'w4_imp`x' w4_imp`x';
rename `var'w5_imp`x' w5_imp`x';


cap rename l`var'w_imp`x' w_imp`x';

cap drop age1_imp`x' age2_imp`x' age3_imp`x' age4_imp`x' age5_imp`x' age6_imp`x' age7_imp`x' age8_imp`x' age9_imp`x';

gen age1_imp`x'=0;
gen age2_imp`x'=0;
gen age3_imp`x'=0;
gen age4_imp`x'=0;

replace age1_imp`x'=1 if age_imp`x'>=3 & age_imp`x'<4.3;

replace age2_imp`x'=1 if age_imp`x'>=4.3 & age_imp`x'<5.5;

replace age3_imp`x'=1 if age_imp`x'>=5.5 & age_imp`x'<6.5;

replace age4_imp`x'=1 if age_imp`x'>=6.5;

replace hh5_imp`x'=1 if hh6_imp`x'==1;

gen risk_imp`x'= riskaver_imp`x'<=2;

foreach y in 2002 /*2005 2008 2011 2014*/ {;
local a=`y'+3;

*global rhsage age1_imp`x' age2_imp`x' age3_imp`x'/* age4_imp`x' age5_imp`x' age6_imp`x' age7_imp`x' age8_imp`x'*/;
global rhsage age_imp`x' agesq_imp`x';
global rhshh hh2_imp`x' hh3_imp`x' hh4_imp`x' hh5_imp`x' /*hh6_imp`x'*/;
*global rhshh hhincome_imp`x' hhincomesq_imp`x';
*global rhshh lincome_imp`x';
global rhsnw w2_imp`x' w3_imp`x' w4_imp`x' w5_imp`x'/**/;
*global rhsnw w_imp`x';

global housingdebts busloan_imp`x' credcard_imp`x' restbis_imp`x';
global busloandebts housing_imp`x' credcard_imp`x' restbis_imp`x';
global credcarddebts housing_imp`x' busloan_imp`x' restbis_imp`x';
global restbisdebts housing_imp`x' busloan_imp`x' credcard_imp`x';

oaxaca ration2_imp`x' (fam: $rhsage univ_imp`x' bhealth_imp`x' sex_imp`x' couplewith_imp`x' couplewithout_imp`x' singlewith_imp`x'  risk_imp`x')
 (income:  $rhshh ) (wealth: $rhsnw) (labour: unemployed_imp`x' retiree_imp`x' selfemployed_imp`x' ) (ration:ration2_imp`x') if year>=`y' & year<=`a', by (year) weight(0) probit svy swap;
	
matrix coefs_`var'`y'_imp`x'=e(b);
matrix vars_`var'`y'_imp`x'=e(V);

};

};




*Compute SE with 5 imputations;

forvalues x = 1(1)5 {;

forvalues p =2002(3)2002 {;
local q=`p'+3;
local j=1;
foreach res in g`q' g`p' dif dch dcf chf chi chw chl chr cof coi cow col cor coc {;

scalar c`p'mean_`var'_`res'_imp`x'=coefs_`var'`p'_imp`x'[1,`j'];
scalar c`p'var_`var'_`res'_imp`x'=vars_`var'`p'_imp`x'[`j',`j'];

scalar sumc`p'mean_`var'_`res'0 = 0;
scalar sumc`p'var_`var'_`res'0 = 0;
scalar sumc`p'N_`var'_`res'0 = 0;
local j=`j'+1;
};
};
};

forvalues p =2002(3)2002{;
local q=`p'+3;
foreach res in g`q' g`p' dif dch dcf chf chi chw chl chr cof coi cow col cor coc {;

forvalues i = 1(1)5 {;
local j = `i' - 1;
scalar sumc`p'mean_`var'_`res'`i' = scalar(sumc`p'mean_`var'_`res'`j') + scalar(c`p'mean_`var'_`res'_imp`i');
scalar sumc`p'var_`var'_`res'`i' = scalar(sumc`p'var_`var'_`res'`j') + scalar(c`p'var_`var'_`res'_imp`i');
*scalar sumc`p'N_`var'_`res'`i' = scalar(sumc`p'N_`var'_`res'`j') + scalar(N`p'_`var'_`res'`i');

} ;

scalar c`p'mean_`var'_`res' = (1/5)*scalar(sumc`p'mean_`var'_`res'5);
scalar c`p'u_`var'_`res' = (1/5)*scalar(sumc`p'var_`var'_`res'5);
scalar c`p'var_`var'_`res'=scalar(c`p'u_`var'_`res')^2;
*scalar c`p'_`q'N_`var'_`res' = (1/5)*scalar(sumc`p'_`q'N_`var'_`res'5);


******************************;
forvalues x = 1(1)5 {;

scalar c`p'msq_`var'_`res'_imp`x'=(c`p'mean_`var'_`res'_imp`x'-scalar(c`p'mean_`var'_`res'))^2;

};

**************;


scalar sumc`p'msq_`var'_`res'0 = 0;

forvalues i = 1(1)5 {;
local j = `i' - 1;
scalar sumc`p'msq_`var'_`res'`i' = sumc`p'msq_`var'_`res'`j' + c`p'msq_`var'_`res'_imp`i';
} ;

scalar c`p'b_`var'_`res'=(1/4)*sumc`p'msq_`var'_`res'5;
scalar c`p't_`var'_`res'=scalar(c`p'u_`var'_`res')+(6/5)*scalar(c`p'b_`var'_`res');
scalar c`p'sqt_`var'_`res'=scalar(c`p't_`var'_`res')^(1/2) /*  STANDARD ERROR */;




scalar c`p'chi2_`var'_`res'=(scalar(c`p'mean_`var'_`res')^2)/(scalar(c`p'sqt_`var'_`res')^2);
scalar c`p'pchi2_`var'_`res'=chi2tail(1,scalar(c`p'chi2_`var'_`res'));
}/* res */;
} /* foreach x in ... */;



forvalues p =2002(3)2002 {;
local q=`p'+3;

*log using "H:\Mi unidad\Grant_new\SERIEs\2nd_revision\probit_year`q'_`p'_`var'.smcl", replace;

foreach res in g`q' g`p' dif dch dcf chf chi chw chl chr cof coi cow col cor coc {;


scalar `res'`p'_`q'_`var'=scalar(c`p'mean_`var'_`res');
scalar se`res'`p'_`q'_`var'=scalar(c`p'sqt_`var'_`res');

di "`res'" "`q' `p'";
di scalar(`res'`p'_`q'_`var');
di scalar(se`res'`p'_`q'_`var');

};
cap log close;
};

	
};
